﻿namespace VLCMusic;

public partial class MainForm : Form
{
    delegate void ResetListHandler();
    delegate void SetTime1Handler(string str);
    delegate void SetTime2Handler(string str);
    delegate void SettrackBarHandler(int str);
    delegate void SetMainTextHandler(string str);
    delegate void SetButton1TextHandler(Image str);
    delegate DialogResult MessageBoxShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);

    readonly Dictionary<string, string> header = new()
    {
        { "Referer", "https://www.9ku.com" },
        { "Host", "www.9ku.com" },
        { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" }
    };
    Size primarySize;
    long playTime;
    long duration;
    int currentIndex = 0;
    List<MusicView> musicList;
    bool fullScreen = false;
    string musicType = "90年代";
    string ids = "";
    bool refresh = false;
    bool mute = false;
    string musicsPath => $"{AppDomain.CurrentDomain.BaseDirectory}Musics\\{musicType}";

    private string TimeToString(double m)
    {
        var dt = new DateTime(1970, 1, 1);
        dt = dt.AddSeconds(m / 1000);
        return dt.Hour.ToString("00") + ":" + dt.Minute.ToString("00") + ":" + dt.Second.ToString("00");
    }
    public MainForm()
    {
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        UpdateStyles();
        InitializeComponent();
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0014) return;
        base.WndProc(ref m);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
    public DialogResult ShowMessage(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
    {
        var dr = new DialogResult();
        try
        {
            var tmp = this.Invoke(new MessageBoxShow(MessageBoxShowF), new object[] { text, caption, buttons, icon });
            if (tmp != null)
            {
                dr = (DialogResult)tmp;
            }
        }
        catch
        {

        }
        return dr;
    }
    DialogResult MessageBoxShowF(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
    {
        var dr = MessageBox.Show(text, caption, buttons, icon);
        return dr;
    }
    public void ResetList()
    {
        if (uiListBox1.IsDisposed)
        {
            return;
        }
        if (uiListBox1.InvokeRequired)
        {
            uiListBox1.Invoke(new ResetListHandler(ResetList));
            return;
        }
        else
        {
            uiListBox1.SelectedIndex = currentIndex;
        }
    }
    public void SetButton1Text(Image str)
    {
        if (pictureBox1.IsDisposed)
        {
            return;
        }
        if (pictureBox1.InvokeRequired)
        {
            pictureBox1.Invoke(new SetButton1TextHandler(SetButton1Text), str);
            return;
        }
        else
        {
            pictureBox1.Image = str;
        }
    }
    public void SetMainText(string str)
    {
        if (this.IsDisposed)
        {
            return;
        }
        if (this.InvokeRequired)
        {
            this.Invoke(new SetMainTextHandler(SetMainText), str);
            return;
        }
        else
        {
            this.Text = str;
        }
    }
    public void SetTime1(string str)
    {
        if (label1.IsDisposed)
        {
            return;
        }
        if (label1.InvokeRequired)
        {
            label1.Invoke(new SetTime1Handler(SetTime1), str);
            return;
        }
        else
        {
            label1.Text = str;
        }
    }
    public void SetTime2(string str)
    {
        if (label2.IsDisposed)
        {
            return;
        }
        if (label2.InvokeRequired)
        {
            label2.Invoke(new SetTime2Handler(SetTime2), str);
            return;
        }
        else
        {
            label2.Text = str;
        }
    }
    public void SettrackBar(int str)
    {
        if (lTrackBar1.IsDisposed)
        {
            return;
        }
        if (lTrackBar1.InvokeRequired)
        {
            lTrackBar1.Invoke(new SettrackBarHandler(SettrackBar), str);
            return;
        }
        else
        {
            lTrackBar1.L_Value = str;
        }
    }
    private void vlcControl1_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
    {
        var currentAssembly = Assembly.GetEntryAssembly();
        var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
        // Default installation path of VideoLAN.LibVLC.Windows
        e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc"));
    }

    private async void Form1_Load(object sender, EventArgs e)
    {
        CommonFun.CreateDir(musicsPath);
        vlcControl1.BackgroundImage = Image.FromFile("logo.ico");
        vlcControl1.BackgroundImageLayout = ImageLayout.Center;
        primarySize = new Size(vlcControl1.Width, vlcControl1.Height);
        label1.Text = "00:00:00";
        label2.Text = "00:00:00";

        lTrackBar2.L_Value = 85;
        vlcControl1.Audio.Volume = 85;

        toolStripStatusLabel1.Text = "音乐来源：九酷音乐（https://www.9ku.com）| 仅供学习交流，严禁用于商业用途，请于24小时内删除~~~ ";
        vlcControl1.EndReached += VlcControl1_EndReached;
        ids = @"56770,37778,69647,29542,209465,183237,42438,63537,1627,11290,74319,37653,463222,74860,176490,66243,64347,402377,66154,64857,34605,178116,59454,74321,24845,78800,176106,78554,
                    74867,49157,75221,362117,92887,79773,176518,46303,75760,65570,185844,57159,464660,175843,508484,75496,563391,185952,80222,93762,557734,183063,208616,670491,224531,78624,88101,447986,80366,468570,411416,641114,55643,67566,44659,507117,417289,199417,183329,199969,
                    84584,82490,76795,79619,52042,3811,417546,439428,206137,670484,184925,399450,415977,426550,454527,471184,670487,335143,463133,85265,469982,574308,200752,365622,176201,184571,37614,410794,76479,464901,62972,95296,471866,54554,365507,25516,76502,173609,74369,82495,
                    198982,414669,67558,409529,75507,654195,13258,409457,69580,75222,670485,468838";
        await LoadMusicListAsync();
    }
    private async void VlcControl1_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
    {
        if (checkBox1.Checked)
        {
            currentIndex = CommonFun.GetRandom(musicList.Count);
            await Task.Run(async () =>
            {
                await PlayMusicAsync(musicList[currentIndex]);
            });
        }
        else
        {
            if (currentIndex + 1 < musicList.Count)
            {
                currentIndex++;
                await Task.Run(async () =>
                {
                    await PlayMusicAsync(musicList[currentIndex]);
                });
            }
            else
            {
                timer1.Stop();
                currentIndex = -1;
                ResetList();
                //vlcControl1.Stop();
                SettrackBar(0);
                SetTime1("00:00:00");
                SetTime2("00:00:00");
                控制ToolStripMenuItem.Text = "播放";
                控制ToolStripMenuItem.Image = Properties.Resources.播放;
                SetButton1Text(Properties.Resources.播放);
                notifyIcon1.Text = CommonFun.ServiceName;
                vlcControl1.BackgroundImage = Image.FromFile("logo.ico");
                vlcControl1.BackgroundImageLayout = ImageLayout.Center;
                SetMainText(CommonFun.ServiceName);
                ShowMessage("播放结束！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }

    private async Task PlayMusicAsync(MusicView music)
    {
        SettrackBar(0);
        SetTime1("缓冲中...");
        SetTime2("00:00:00");
        currentIndex = musicList.IndexOf(music);
        ResetList();
        vlcControl1.ResetMedia();
        if (music.zjpic.NotNull() && music.zjpic.Contains("http"))
        {
            vlcControl1.BackgroundImage = Image.FromStream(new MemoryStream(await HttpUtil.HttpGetByteAsync(music.zjpic, null)));
            vlcControl1.BackgroundImageLayout = ImageLayout.Stretch;
        }
        else if (music.gspic.NotNull() && music.gspic.Contains("http"))
        {
            vlcControl1.BackgroundImage = Image.FromStream(new MemoryStream(await HttpUtil.HttpGetByteAsync(music.gspic, null)));
            vlcControl1.BackgroundImageLayout = ImageLayout.Stretch;
        }
        else
        {
            vlcControl1.BackgroundImage = Image.FromFile("logo.ico");
            vlcControl1.BackgroundImageLayout = ImageLayout.Center;
        }
        SetMainText($"{CommonFun.ServiceName}  |（{musicType}）{music.mname} - {music.singer} -《{music.zjname}》| 当前播放：{currentIndex + 1}/{musicList.Count} 首");
        notifyIcon1.Text = $"{music.mname}（{music.singer}） - {CommonFun.ServiceName}";
        var filename = $"{musicsPath}\\{music.mname.Replace("/", "")} - {music.singer}.{music.wma.Split('/').Last().Split('.')[1]}";
        if (!File.Exists(filename))
        {
            refresh = true;
            var bytes = await HttpUtil.HttpGetByteAsync(music.wma, null);
            vlcControl1.SetMedia(new MemoryStream(bytes));
            using var fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
            fs.Write(bytes, 0, bytes.Length);
        }
        else
        {
            vlcControl1.SetMedia(new FileInfo(filename));
        }
        pictureBox1_Click(null, null);
        ReloadMusicList();
    }

    private void ReloadMusicList()
    {
        if (refresh)
        {
            foreach (var item in musicList)
            {
                var filename = $"{musicsPath}\\{item.mname.Replace("/", "")} - {item.singer}.{item.wma.Split('/').Last().Split('.')[1]}";
                if (File.Exists(filename))
                {
                    item.musicname = $"[本地] {item.mname.Replace("/", "")} - {item.singer}";
                }
                else
                {
                    item.musicname = $"{item.mname.Replace("/", "")} - {item.singer}";
                }
            }
            uiListBox1.DataSource = null;
            uiListBox1.DataSource = musicList;
            uiListBox1.DisplayMember = "musicname";
            uiListBox1.ValueMember = "wma";
            refresh = false;
        }
    }

    private async void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        try
        {
            var music = (MusicView)uiListBox1.SelectedItem;
            await PlayMusicAsync(music);
        }
        catch (Exception)
        {
            MessageBox.Show("歌曲获取失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        duration = vlcControl1.Length;
        playTime = vlcControl1.Time;
        if (duration > 0)
        {
            label1.Text = TimeToString(vlcControl1.Time);
            label2.Text = TimeToString(duration);
            if (playTime > duration)
            {
                label1.Text = "00:00:00";
                pictureBox1.Image = Properties.Resources.播放;
                lTrackBar1.L_Value = 0;
                timer1.Stop();
            }
            else
            {
                var value = playTime * 10000 / duration;
                lTrackBar1.L_Value = (int)value;
            }
        }
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (fullScreen)
        {
            if (keyData == Keys.Escape)
            {
                FormBorderStyle = FormBorderStyle.Sizable;
                WindowState = FormWindowState.Normal;
                vlcControl1.ClientSize = primarySize;
                vlcControl1.Dock = DockStyle.None;
                fullScreen = false;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    private void vlcControl1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (!fullScreen)
        {
            statusStrip1.Visible = false;
            menuStrip1.Visible = false;
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            System.Drawing.Rectangle ret = Screen.GetWorkingArea(this);

            vlcControl1.ClientSize = new Size(ret.Width, ret.Height);
            vlcControl1.Dock = DockStyle.Fill;
            vlcControl1.BringToFront();
            fullScreen = true;
        }
        else
        {
            statusStrip1.Visible = true;
            menuStrip1.Visible = true;
            FormBorderStyle = FormBorderStyle.Sizable;
            WindowState = FormWindowState.Normal;
            vlcControl1.ClientSize = primarySize;
            vlcControl1.Dock = DockStyle.None;
            fullScreen = false;
        }
    }

    private void 打开目录ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            var psi = new ProcessStartInfo() { FileName = musicsPath, UseShellExecute = true };
            Process.Start(psi);
        }
        catch (Exception)
        {
            MessageBox.Show("未找到歌曲目录！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void 关于软件ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show($"{CommonFun.ServiceName}\r\nPowered by 昨夜丶雨疏风骤\r\n音乐来源：九酷音乐（https://www.9ku.com）\r\n仅供学习交流，严禁用于商业用途，请于24小时内删除~~~ ", "关于软件", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void 帮助文档ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "help.pdf"));
        }
        catch (Exception)
        {
            MessageBox.Show("操作失败，未找到帮助文档或未安装pdf阅读器！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private async Task LoadMusicListAsync()
    {
        CommonFun.CreateDir(musicsPath);
        var param = "https://www.9ku.com/playlist.php";
        var resut = await HttpUtil.HttpPostAsync(param, "ids=" + ids, header, contentType: "application/x-www-form-urlencoded");
        musicList = resut.ToObject<List<MusicView>>();
        foreach (var item in musicList)
        {
            var filename = $"{musicsPath}\\{item.mname.Replace("/", "")} - {item.singer}.{item.wma.Split('/').Last().Split('.')[1]}";
            if (File.Exists(filename))
            {
                item.musicname = $"[本地] {item.mname.Replace("/", "")} - {item.singer}";
            }
            else
            {
                item.musicname = $"{item.mname.Replace("/", "")} - {item.singer}";
            }
        }
        uiListBox1.DataSource = musicList;
        uiListBox1.DisplayMember = "musicname";
        uiListBox1.ValueMember = "wma";
        await PlayMusicAsync(musicList[currentIndex]);
    }

    private async void 华语经典ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "华语经典")
        {
            currentIndex = 0;
            musicType = "华语经典";
            ids = @"1462,1903,11417,30001,42620,59930,64541,65937,66417,56770,81667,1601,35721,82151,179782,199353,24865,187601,408690,91161,48517,27247,37778,11309,649,9762,2247,671,183237,1545,22985,1456,69647,65322,67699,64291,20753,209465,29542,76536,4822,81674,29215,3954,20823,69933,42474,463222,66243,81655,38248,34605,75802,64974,74372,24118,64857,63978,11895,11411,4070,67731,53508,72066,59454,1656,53431,64496,65410,24371,9411,42702,58854,65020,78068,74860,12194,75689,4112,67984,
                        4822,81674,29215,3954,20823,69933,42474,463222,66243,81655,38248,34605,75802,64974,74372,24118,64857,63978,11895,11411,4070,67731,53508,72066,59454,1656,53431,64496,65410,24371,9411,42702,58854,65020,78068,74860,12194,75689,4112,67984,
                        75100,21074,74184,65865,74319,49182,69580,74321,75859,64280,72067,176106,53860,81653,74936,52028,45258,24420,75201,194548,58715,52079,462675,56499,74867,86147,361430,42438,57349,58789,73037,73053,55407,1314,75507,72069,65321,34026,75694,75196,
                        28784,79930,1670,73125,175930,84348,12102,38979,81668,54972,11290,1667,30297,78800,20014,75730,1568,20013,61477,84570,62878,86322,75586,1602,78347,1332,67558,62665,12971,64489,91500,2772,1627,22621,76626,76494,1450,14998,24758,1529,
                        35504,78554,11261,59205,61171,87568,23251,185870,41815,11730,26061,103,600,55837,19117,78064,57937,188436,72636,77062,41813,11315,176490,72008,4831,84216,45262,20016,63537,65584,85682,64533,1527,184891,281937,1333,1725,30489,11271,15920,
                        64894,46926,1658,4426,42111,3946,1850,52981,23527,43234,39933,64225,53059,47071,65086,36498,29009,30820,39565,356857,61070,1841,1835,456637,46551,57159,71143,3950,58906,64540,173876,21982,90304,50,11284,50901,58805,176214,38937,176318,
                        54769,29821,55838,191650,15421,65076,12164,4984,11553,5790,58714,574593,29210,11312,11069,186140,64347,62867,20017,11061,52279,1604,78072,184700,401530,56577,49157,1638,3952,67693,76670,592,360221,40952,1380,22833,4254,22588,11934,13244,
                        11933,11926,46949,13258,4140,73652,54597,22977,22065,3026,65226,48366,11515,1570,49148,67561,81643,61092,76921,37894,12492,23631,26942,65939,58020,36803,11936,44208,65373,83813,11316,185844,64997,40963,50957,187449,189802,36634,11446,42030,
                        62699,44225,1308,409457,58718,58864,35093,1417,75222,91002,63515,414669,46303,10897,91166,75760,50576,24693,436,35716,48336,4193,75587,2458,63507,3057,305840,79773,64211,62890,2174,63213,75221,51942,514301,79772,88340,92887,53830,38107,
                        50300,1556,22658,1415,53753,9408,15826,65936,79121,61249,409778,9397,1864,67913,218653,86457,197106,91126,197277,466375,38935,29160,13239,78905,64051,51878,47751,46293,305842,57915,46294,22205,11408,58028,53502,83450,174353,57940,59967,65028,
                        78070,57629,354937,409529,11666,54975,35297,203983,42028,1612,81474,57335,91163,1677,68827,4385,58021,26147,72070,46299,82863,1306,62900,57927,50302,48258,6682,62892,53473,46912,84682,11489,80222,2637,9394,59736,75244,55606,1544,22974,
                        58128,61591,5800,11658,11406,198982,367561,38370,34192,11277,85262,33584,68957,32906,62975,20481,24845,2169,58859,178873,105,53826,361311,69619,32899,467071,57312,44335,81652,45263,45240,2773,78646,2362,96616,3599,49180,64946,59457,38001,
                        30487,35717,22585,46272,12195,11435,1534,28261,75496,69607,197039,1317,184890,27585,176211,400156,26080,9118,57925,55840";
            await LoadMusicListAsync();
        }
    }

    private async void 粤语经典ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "粤语经典")
        {
            currentIndex = 0;
            musicType = "粤语经典";
            ids = @"42620,671,30001,649,48517,24420,81655,103,20823,34026,39565,38937,53867,40952,54769,53860,1415,456637,1450,42111,35297,53753,66154,57937,44245,38388,35504,11315,22658,11692,3675,50576,1417,11316,48336,105,57940,57927,22833,5334,57925,57915,48733,44335,11761,26375,52033,19823,38935,458802,41623,30015,58011,68815,75194,11204,44518,53877,35308,35509,43118,11365,53459,53760,1843,11364,30686,11494,45078,33964,54772,48159,308541,27320,29685,50642,44078,1828,22989,21800,54288,54031,57689,33973,45436,33398,19824,44243,58163,45278,35296,30458,45405,224807,79622,45445,35305,48211,6634,58010,45265,45451,24130,44946,58007,10848,6654,66152,57173,71472,45492,52603,3269,49394,27154,101,10852,25667,37261,24124,57696,37480,44327,6712,52584,45060,27177,39398,851915,50658,3308,851922,42189,53397,851913,55757,53739,44576,53888,308540,463702,34666,43251,56355,213709,568362,21306,26768,851917,44344,361325,37742,851903,45099,44341,29190,42177,22698,5212,27327,10285,72506,39388,44945,851908,53107,851925,52929,37566,851811,512970,22692,851898,39066,22697,37744,44525,45111,851812,4279,851907,412912,328601,40807,44521,40686,58900,39052,179498,851910,4299,22164,23304,39231,64782,39357,71445,36718,851808,5080";
            await LoadMusicListAsync();
        }
    }

    private async void 闽南经典ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "闽南经典")
        {
            currentIndex = 0;
            musicType = "闽南经典";
            ids = @"66417,20753,21944,514301,12766,55679,47751,358185,25854,332017,31537,12765,12759,12758,354319,12763,23757,12762,206866,43186,53280,12764,93237,47417,74457,342428,25864,355592,12775,355597,47024,91139,218627,26156,12773,332040,342437,23354,321636,177665,47416,332216,1912,22464,47756,183444,274197,92850,406978,218622,33308,23755,342429,332031,206392,91160,330946,74452,416793,73784,47439,206870,543563,71043,73777,71032,47422,71033,61516,355598,74451,47426,332020,180595,94291,309908,179588,47025,330926,562729,94292,510522,7777,332213,321641,85434,355615,73076,180597,332212,513830,336541,41976,91156,94294,80532,89782,184110,47438,186015,309929,548024,92970,365626,50765,57439,332210,23759,73779,331663,548022,355605,85813,174174,47752,335243,572094,177663,637246,316042,335248,89019,355620,23358,94298,203158,335268,246204,186932,365630,47428,332202,564489,90141,73783,73787,336503,177686,183390,336509,90140,186936,73788,180599,179584,336542,43181,548026,177664,180593,71039,49293,335254,335261,334891,94296,183439,203164,334907,180598,71040,71041,80536,332220,177681,334882,362795,47029,156869,177662,365628,2920,28405,300586,300596,47757,80533,203165,328430,466334,177688,183392,43397,47030,3340,218657,565556,355595,334911,86097,89015,177667,92972,335126,1346,2448,73778,183440,565767,365632";
            await LoadMusicListAsync();
        }
    }

    private async void dJ舞曲ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "DJ舞曲")
        {
            currentIndex = 0;
            musicType = "DJ舞曲";
            ids = @"60049,664643,855155,499429,469902,60052,492097,657342,384076,657336,383989,375902,995587,494868,643245,660650,375816,470111,314534,496304,375800,469973,499698,384040,488687,384191,496646,643433,60059,496667,579209,496896,462918,375792,74086,496428,926934,408442,496715,376781,375795,376782,375780,60065,69117,853408,827032,45773,375988,408440,853497,642411,60054,500413,496904,229432,661857,469849,60056,470085,470123,60064,470037,500013,496723,374668,376087,642410,660654,375788,376877,175110,853691";
            await LoadMusicListAsync();
        }
    }

    private async void 轻音乐ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "轻音乐")
        {
            currentIndex = 0;
            musicType = "轻音乐";
            ids = @"381049,437663,381056,458816,400027,13036,289219,430708,444324,289165,394188,645517,289480,434806,461011,458360,443892,426952,453599,289043,457746,289491,401483,13072,289619,197342,197927,13034,539794,331283,213014,381151,289070,289351,381128,351151,456157,81807,540144,419037,423365,381159,835088,596219,451828,835086,381177,835097,345527,459676,456305,451430,835110,180105,214527,214533,426951,436189,30587,430727,183551,458906,458813,417404,425256,197489,82988,458809,441732,645926,661632,434633,645348,439274,83006,197401,467253,539780,509299,442742,656928,429935,430449,422414,459580,641242,854845,539927,540129,539861,660258,649754,870287,197217,289141,664750,452713,664743,454264,664664,639008,650373";
            await LoadMusicListAsync();
        }
    }

    private async void 影视金曲ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "影视金曲")
        {
            currentIndex = 0;
            musicType = "影视金曲";
            ids = @"81667,74376,61080,75209,64543,185956,64330,70561,471,476,469985,70450,76798,70467,69852,64334,94054,7423,470,7432,69485,210336,313351,306765,503869,413614,183900,64544,7429,185525,853205,827338,853337,183895,306760,204591,852830,306749,7302,853201,
                        42633,306755,282847,851868,11979,306752,6164,409080,472,852860,7447,289825,590786,369684,87081,183896,854043,441186,851815,545218,306747,78866,480,463976,7188,854154,289822,481,7368,179303,185522,855883,313340,855533,852810,7446,851888,64333,337351,479,
                        563888,853512,48892,505540,422671,7440,853514,661420,6227,853640,855379,506907,233,430144,855138,424286,854548,475,447696,302629,857230,856707,423894,834914,443511,289829,204592,2906,374434,856942,234105,855489,669448,852528,856092,410624,856157,853508,599070,78871,
                        319657,223998,374432,599069,856158,851819,79365,654809,665892,78886,599064,368585,857708,854306,664940,856144,513723,548061,507344,513726,856523,409103,337805,856519,79367,78893,665341,668106,69848,565912,574134,563560,853498,88222,337742,857392,79369,287102,548075,856389,
                        6159,852280,302628,270668,863109,649497,598403,181996,588509,79368,69845,174092,294,666514,79372,858676,561799,854550,656612,545890,857389,598407,829621,598405,226342,854046,661284,663325,204702,512663,187,572811,854047,598406,39918,560688,565631,857316,590136,595625,
                        861075,858330,567097,464479,590693,661285,602940,856438,860855,320446,586926,470286,598404,313367,606401,853726,467112,289849,464053,853929,374544,872726,545888,613571,470315,863543,567206,859779,177,576495,858677,285722,858688,26997";
            await LoadMusicListAsync();
        }
    }

    private async void kTV金曲ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "kTV金曲")
        {
            currentIndex = 0;
            musicType = "kTV金曲";
            ids = @"64541,11417,56770,1601,9762,24865,24420,1545,464446,183237,69933,79882,179782,76536,2772,1568,637791,62665,1627,1658,397945,1570,59205,65410,1725,26061,456637,49182,3946,1656,183203,361140,402377,81016,12102,68772,40963,365138,74184,84566,
                    186947,473312,21982,184891,48366,417329,55837,59967,505193,75802,58805,56023,1841,15421,29009,56012,11515,187052,56577,184890,11316,408736,365136,14422,179483,1602,2174,2169,86179,1638,414669,465403,1544,411603,65865,75859,55827,416535,75762,183636,
                    409778,86322,183176,398849,173981,464181,28261,176518,75845,465288,62699,43234,75760,69532,23563,178725,208128,49180,61103,467927,412813,464660,175843,57335,574593,408732,408739,185952,81018,58021,91126,469728,414200,81306,82897,4984,82896,2773,74378,638353,
                    599206,59736,410632,87010,361322,409428,415949,506703,199040,62900,198681,182525,56017,473369,199417,70562,188060,183329,412135,79121,355148,203453,64275,84584,11780,85967,408887,21999,455894,576910,383348,41827,78506,182527,1580,184925,175129,61107,69881,181940,
                    174773,472229,414643,413460,65867,184629,351666,80479,471184,464205,564276,329847,184790,184554,82464,182466,365622,188362,76791,469756,203813,410794,364232,95296,413307,186747,464448,462883,365507,175661,184127,76598,365508,90549,10726,409684,461547,212778,413752,81058";
            await LoadMusicListAsync();
        }
    }

    private async void 一人一首成名曲ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "一人一首成名曲")
        {
            currentIndex = 0;
            musicType = "一人一首成名曲";
            ids = @"1462,1234703,875689,41813,42620,1004523,1903,1219032,59930,64541,35721,11417,56770,37778,1379392,671,22985,69647,65937,29542,1217839,27247,66417,1601,9762,64291,892204,1003659,24865,82151,
                    48517,24420,4822,81667,1545,1235568,20753,11309,1010051,11895,1378638,65322,464446,408690,199353,667326,9411,81655,1379761,103,3954,183237,20823,69933,1219189,34026,42702,187601,1001197,79882,
                    62878,39565,11271,42438,29215,411767,76536,11730,2772,64974,1191283,665180,78392,667033,58854,1568,63537,637791,79930,62665,1333,642431,53867,40952,57349,504684,1013603,20014,1627,361430,
                    54769,28784,397945,863755,665896,1725,1002010,53508,1012453,855488,463222,52279,194548,53830,12194,81668,58789,12971,1006548,592,176490,81674,1670,66243,21074,64347,42111,1314,851853,81016,
                    23251,68772,669503,36498,1012191,862272,4426,655864,68957,64857,3028,853199,34605,71143,30297,29821,853316,670270,1060864,863848,59454,668042,669484,829596,186947,30489,669312,53431,885006,668991,
                    29160,41266,67731,64280,853045,64540,860416,22658,1001691,78800,19117,75689,1004430,10897,84570,890625,882935,884206,75201,857925,176969,24693,81653,66070,37894,198982,187052,50576,1051270,4385,
                    1217858,468311,35093,84216,367229,22833,1527,5334,367561,409529,86147,411603,416535,600306,864424,69580,75222,835088,54972,994784,4193,881513,468838,999604,65086,74936,84348,456305,187390,43957,
                    1005633,401530,661425,173981,197277,175415,568426,281937,204001,857708,827347,928190,3368,78347,407509,305840,1015906,465288,999489,1093787,1234810,33246,27585,53473,607175,861336,22588,178725,52981,188203,
                    998941,2458,852134,314534,1011414,8192,275277,57335,12766,85262,1009021,180247,49772,12164,80860,1935,65373,408739,174353,13855,57312,890196,56781,185952,545218,1083791,91126,507680,75124,46355,
                    512795,81306,82897,4984,22065,90304,643297,179214,646814,225967,179404,57689,354745,858744,421176,878081,29051,4762,1012035,887835,868281,11559,63319,30458,1020719,3057,185738,44946,827491,469870,
                    40876,506703,186808,199040,854916,12143,287768,995311,893352,664617,513871,198681,1214873,1001319,22233,95061,368604,70590,1010047,1235262,1005157,5308,213099,511492,1010492,411976,185843,70562,466999,568853,
                    851915,883762,601638,317745,85180,59650,636456,70576,635970,891238,95215,1004316,514346,302780,458256,1021221,356010,355172,63290,639734,1219263,198682,173657,464661,997610,569236,855858,1011941,81650,463903,
                    33556,1001507,853155,183873,296393,1152369,861935,1049665,64539,274778,89678,1012957,504897,70575,635927,215250,26045,1011078,1010023,1066161,1217803,412325,589378,1005173,591295,21056,641217,211114,1182661,589435,
                    565716,465260,471467,1056602,416739,1236317,410018,559986,651278,1010394,589391,328298,213094,886403,1010110,1010232,873310,397481,853640,1000496,4692,1129879,411452,661889,85822,177722,364232,1010653,65365,1015687,
                    5896,182999,308837,865721,853188,834941,1114233,667194,1075673,998960,15867,656325,1084740,872714,1172913,657378,34925,652715,666863,659190,592709,29708,650823,323856,892814,926897,858330,308740,352403,857392,
                    182763,345821,1059551,515482,1011734,569568,869098,1188988,81683,874394,892527,669669,273469,1010717,856059,881163,596519,853524,173997,556792,558043,358505,671092,248388,1386744,871361,665757,459235,1130716,855370,
                    1093712,995889,173892,881849,827306,1131139,643920,599981,1215303,1129075,1387875,28353,1173914,292846,338335,1383655,1384056,15842,1386245,546113,1235259,876504";
            await LoadMusicListAsync();
        }
    }

    private async void 心碎情歌ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "心碎情歌")
        {
            currentIndex = 0;
            musicType = "心碎情歌";
            ids = @"22985,29542,464446,76536,79930,24118,57349,78072,65020,34605,11315,670346,186947,417329,11061,11926,30820,198982,1602,411603,569801,187390,93483,362117,409332,46303,48666,23563,178725,58020,38365,416563,473475,13244,79103,46949,82863,50302,80753,12492,
                    471647,409506,2637,182737,74378,90418,463194,90304,78624,58028,63149,76921,36803,562902,559338,42028,70901,557455,94207,33824,78401,574384,77101,3599,651810,182966,85948,62975,68827,64275,32922,61537,13224,31349,566179,88528,81808,470615,82858,407510,
                    408713,64526,212042,78585,80479,198892,4813,564632,75412,76321,9282,74487,183248,56813,412434,184956,663023,95568,42501,188118,658649,664588,564100,462239,663979,666290,186979,515442,664065,409526,468152,411605,223542,82581,853551,663094,573003,95363,19118,827601";
            await LoadMusicListAsync();
        }
    }

    private async void 日韩经典ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (musicType != "日韩经典")
        {
            currentIndex = 0;
            musicType = "日韩经典";
            ids = @"664618,664617,513871,181384,663895,664271,661889,664612,663902,446972,663894,208178,213068,208736,662528,276092,664262,664609,415839,661808,663901,462618,15123,81837,465059,664789,661888,411277,664604,404981,275745,266378,662494,85902,400624,662536,664786,83653,185882,656473,
                    198786,443621,467994,663900,312871,440928,456072,210738,645729,428169,212886,177065,670818,275750,664606,671061,506224,210772,276087,314280,664605,670813,670815,660064,82333,466888,664267,215825,647205,294967,601508,87471,665022,664261,649092,664263,664788,664790,509169,47893,
                    209053,82853,187082,664285,664575,657229,210541,662533,664619,656477,658825,664791,532973,213607,81052,664792,546017,664610,660934,662477,228049,218581,656131,65059,670816,81039,664277,593117,664569,316414,213249,505601,82706,76566,647213,290366,670817,61283,664714,662155,
                    664719,220815,251281,653718,670889,87726,213687,93374,665028,660933,664781,664784,664793,664571,664829,593153,664578,427798,466277,641133,512881,651124,649090,664716,664718,355729,509843,81047,546479,654529,227525,664778,664780,664787,547809,440316,26880,433440,411178,664620,
                    411181,566090,670811,670812,61278,651127,276103,211081,181135,662678,466882,467158,664281,664283,546011,596451,81904,404980,347638,664577,651022,222223,76560,76567,664601,503071,664608,664611,647212,215089,210486,229953,654447,347782,649094,403851,81038,431771,82849,648866,
                    595632,664770,664259,407239,664782,664785,664278,664284,547807,662497,210666,469743,532981,275711,662540,664607,644133,654911,466272,512876,220814,411279,220317,85930,652463,546484,665024,664772,664264,664268,664269,664270,407247,218580,601047,347632,664573,664581,86027,347677,
                    564006,546347,290347,657196,546348,215090,542770,661556,290356,83769,637757,654912,603207,603209,603210,664666,664667,664668,664670,404062,664671,69219,466278,641130,332139,511344,512880,641137,332148,210549,210550,543608,332159,825988,825989,649095,570250,664715,664717,411278,
                    181138,251283,181140,181141,181142,266392,414618,187802,414620,549277,648864,209058,82851,648868,229036,432813,90542,532401,533937,229043,533940,652468,533175,665017,93370,660927,664255,210623,543423,664256,664257,664769,664258,664260,407236,664773,664774,664776,664777,407241,
                    664779,418763,407244,407246,664783,395984,664272,664274,664279,470743,543191,664280,664282,648666,593116,662495,316383,547808,261861,34287,347644,420860,275709,548606";
            await LoadMusicListAsync();
        }
    }

    private void 控制ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (currentIndex > -1)
        {
            var state = vlcControl1.State;
            if (state == MediaStates.Playing)
            {
                timer1.Stop();
                vlcControl1.Pause();
                控制ToolStripMenuItem.Text = "播放";
                控制ToolStripMenuItem.Image = Properties.Resources.播放;
                SetButton1Text(Properties.Resources.播放);
            }
            else
            {
                timer1.Start();
                vlcControl1.Play();
                控制ToolStripMenuItem.Text = "暂停";
                控制ToolStripMenuItem.Image = Properties.Resources.暂停;
                SetButton1Text(Properties.Resources.暂停);
            }
        }
        else
        {
            MessageBox.Show("双击播放歌曲！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Dispose();
        Environment.Exit(0);
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Show();
        WindowState = FormWindowState.Normal;
        Focus();
    }

    private void MainForm_Resize(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.BalloonTipText = "双击打开主界面";
            notifyIcon1.BalloonTipTitle = CommonFun.ServiceName;
            notifyIcon1.ShowBalloonTip(100);
            Hide();
        }
    }

    private async void 下一首ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            currentIndex = CommonFun.GetRandom(musicList.Count);
            await PlayMusicAsync(musicList[currentIndex]);
        }
        else
        {
            if (currentIndex + 1 < musicList.Count)
            {
                currentIndex++;
            }
            else
            {
                currentIndex = 0;
            }
            await PlayMusicAsync(musicList[currentIndex]);
        }
        控制ToolStripMenuItem.Text = "暂停";
        控制ToolStripMenuItem.Image = Properties.Resources.暂停;
        SetButton1Text(Properties.Resources.暂停);
    }

    private async void 上一首ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            currentIndex = CommonFun.GetRandom(musicList.Count);
            await PlayMusicAsync(musicList[currentIndex]);
        }
        else
        {
            if (currentIndex < 1)
            {
                currentIndex = musicList.Count - 1;
            }
            else
            {
                currentIndex--;
            }
            await PlayMusicAsync(musicList[currentIndex]);
        }
        控制ToolStripMenuItem.Text = "暂停";
        控制ToolStripMenuItem.Image = Properties.Resources.暂停;
        SetButton1Text(Properties.Resources.暂停);
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        vlcControl1.Stop();
        lTrackBar1.L_Value = 0;
        label1.Text = "00:00:00";
        label2.Text = "00:00:00";
        pictureBox1.Image = Properties.Resources.播放;
        currentIndex = -1;
        uiListBox1.SelectedIndex = -1;
        vlcControl1.BackgroundImage = Image.FromFile("logo.ico");
        vlcControl1.BackgroundImageLayout = ImageLayout.Center;
        Text = CommonFun.ServiceName;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (currentIndex > -1)
        {
            var state = vlcControl1.State;
            if (state == MediaStates.Playing)
            {
                timer1.Stop();
                vlcControl1.Pause();
                控制ToolStripMenuItem.Text = "播放";
                控制ToolStripMenuItem.Image = Properties.Resources.播放;
                SetButton1Text(Properties.Resources.播放);
            }
            else
            {
                timer1.Start();
                vlcControl1.Play();
                控制ToolStripMenuItem.Text = "暂停";
                控制ToolStripMenuItem.Image = Properties.Resources.暂停;
                SetButton1Text(Properties.Resources.暂停);
            }
        }
        else
        {
            MessageBox.Show("双击播放歌曲！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private async void pictureBox5_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            currentIndex = CommonFun.GetRandom(musicList.Count);
            await PlayMusicAsync(musicList[currentIndex]);
        }
        else
        {
            if (currentIndex + 1 < musicList.Count)
            {
                currentIndex++;
            }
            else
            {
                currentIndex = 0;
            }
            await PlayMusicAsync(musicList[currentIndex]);
        }
    }

    private async void pictureBox4_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            currentIndex = CommonFun.GetRandom(musicList.Count);
            await PlayMusicAsync(musicList[currentIndex]);
        }
        else
        {
            if (currentIndex < 1)
            {
                currentIndex = musicList.Count - 1;
            }
            else
            {
                currentIndex--;
            }
            await PlayMusicAsync(musicList[currentIndex]);
        }
    }

    private void lTrackBar1_MouseMove(object sender, MouseEventArgs e)
    {
        timer1.Stop();
        vlcControl1.Time = lTrackBar1.L_Value * duration / 10000;
        timer1.Start();
    }

    private void lTrackBar2_MouseMove(object sender, MouseEventArgs e)
    {
        vlcControl1.Audio.Volume = lTrackBar2.L_Value;
    }

    private void lTrackBar2_LValueChanged(object sender, ControlEx.LEventArgs e)
    {
        vlcControl1.Audio.Volume = (int)e.Value;
        vlcControl1.Audio.IsMute = false;
        mute = false;
        pictureBox3.Image = Properties.Resources.音量;
    }

    private async void uiListBox1_ItemDoubleClick(object sender, EventArgs e)
    {
        try
        {
            var music = (MusicView)uiListBox1.SelectedItem;
            await PlayMusicAsync(music);
        }
        catch (Exception)
        {
            MessageBox.Show("歌曲获取失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void pictureBox3_Click(object sender, EventArgs e)
    {
        if (mute)
        {
            vlcControl1.Audio.IsMute = false;
            mute = false;
            pictureBox3.Image = Properties.Resources.音量;
        }
        else
        {
            vlcControl1.Audio.IsMute = true;
            mute = true;
            pictureBox3.Image = Properties.Resources.静音;
        }
    }
}